package com.java.springbootwatermark.controller;

import com.java.springbootwatermark.pojo.ImageInfo;
import com.java.springbootwatermark.service.ImageUploadService;
import com.java.springbootwatermark.service.ImageWatermarkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Map;

@Controller
public class WatermarkController {
    private final ResourceLoader resourceLoader;

    @Autowired
    public WatermarkController(ResourceLoader resourceLoader) {
        this.resourceLoader = resourceLoader;
    }

    @Autowired
    private ImageUploadService imageUploadService;

    @Autowired
    private ImageWatermarkService watermarkService;

    @Value("${web.upload-path}")
    private String path;

    @RequestMapping(value = "/watermarktest", method = RequestMethod.POST)
    public ImageInfo watermarkTest( @RequestParam("file") MultipartFile image ) {

        ImageInfo imgInfo = new ImageInfo();

        //String uploadPath = "static/";  // 服务器上上传文件的相对路径
        //String physicalUploadPath = getClass().getClassLoader().getResource(uploadPath).getPath();  // 服务器上上传文件的物理路径

//        String realPath = "D:\\test";

       // String imageURL = imageUploadService.uploadImage( image, uploadPath, physicalUploadPath );
        String imageURL = imageUploadService.uploadImage( image, path, path );
       // File imageFile = new File(physicalUploadPath + image.getOriginalFilename() );
        File imageFile = new File(path + image.getOriginalFilename() );

//        String watermarkAddImageURL = watermarkService.watermarkAdd(imageFile, image.getOriginalFilename(), uploadPath, physicalUploadPath);
        String watermarkAddImageURL = watermarkService.watermarkAdd(imageFile, image.getOriginalFilename(), path, path);

        imgInfo.setImageUrl(imageURL);
        imgInfo.setLogoImageUrl(path + "/" + watermarkAddImageURL);
        return imgInfo;
    }



    /**
     * 跳转到文件上传页面
     * @return
     */
    @RequestMapping("test")
    public String toUpload(){
        return "test";
    }

    @RequestMapping(value = "watermarkftl", method = RequestMethod.POST)
    public String watermark(@RequestParam("fileName") MultipartFile file, Map<String, Object> map) {

        // 上传成功或者失败的提示
        String msg = "";

        String realPath = "D:/test";

        String imageURL = imageUploadService.uploadImage( file, realPath, realPath );
        File imageFile = new File(realPath + file.getOriginalFilename() );
        if (imageFile.exists()) {
            // 上传成功，给出页面提示
            msg = "上传成功！";
            String watermarkAddImageURL = watermarkService.watermarkAdd(imageFile, file.getOriginalFilename(), realPath, realPath);
            map.put("fileName", watermarkAddImageURL);
        }else {
            msg = "上传失败！";
        }


        // 显示图片
        map.put("msg", msg);
        //map.put("fileName", file.getOriginalFilename());

        return "forward:/test";
    }

    /**
     * 显示单张图片
     * @return
     */
    @RequestMapping("show")
    public ResponseEntity showPhotos(String fileName){

        try {
            // 由于是读取本机的文件，file是一定要加上的， path是在application配置文件中的路径
            return ResponseEntity.ok(resourceLoader.getResource("file:" + path + fileName));
        } catch (Exception e) {
            return ResponseEntity.notFound().build();
        }
    }
}